package common.panel
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.core.UIComponent;
	
	import spark.components.Panel;
	
	[Event(name="restore")]  
	[Event(name="maximize")]  
	public class FlexiblePanel extends Panel  
	{  
		private var state:int =0;//代表是否被最大化  
		private var btStateUp:Button;//可以单击该按钮将Panel最大化  
		private var btStateDown:Button;//可以单击该按钮将Panel还原  
		
		private var tWidth:int;
		private var tHeight:int;
		[Embed("../../imgResources/panel_tool_expand.gif")]  
		private var buttonUpIcon:Class;  
		
		[Embed("../../imgResources/panel_tool_collapse.gif")]  
		private var buttonDownIcon:Class;  
		//设置状态，看看派发哪个事件  
		private function setState(state:int):void{  
			this.state = state;  
			if(state == 0){  
				this.dispatchEvent(new Event("restore"));  
			} else {  
				this.dispatchEvent(new Event("maximize"));  
			}  
		}  
		protected override function createChildren():void{//重写Panel中的createChildren  
			super.createChildren();  
			btStateUp = new Button();  
			btStateDown = new Button();  
			btStateUp.addEventListener("click",doMaximize);  
			btStateDown.addEventListener("click",doRestore);  
			btStateUp.setStyle("overIcon",buttonUpIcon);  
			btStateUp.setStyle("downIcon",buttonUpIcon);  
			btStateUp.setStyle("upIcon",buttonUpIcon);  
			btStateDown.setStyle("overIcon",buttonDownIcon);  
			btStateDown.setStyle("downIcon",buttonDownIcon);  
			btStateDown.setStyle("upIcon",buttonDownIcon);  
			btStateUp.visible = true;  
			btStateDown.visible = false;  
			
			var ui:UIComponent = new UIComponent();
			ui.addChild(btStateUp);
			ui.addChild(btStateDown);
			this.addElement(ui);
			//rawChildren.addChild(btStateUp);  
			//rawChildren.addChild(btStateDown);  
		}  
		//重写定义小图标的位置和大小  
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeigh:Number):void{  
			super.updateDisplayList(unscaledWidth,unscaledHeigh);  
			if(unscaledWidth > 0){  
				this.visible = true;  
			} else {  
				this.visible = false;  
			}  
			var upAsset:DisplayObject = btStateUp.getChildByName("upIcon");  
			var downAsset:DisplayObject = btStateDown.getChildByName("upIcon");  
			var margin:int = 4;//详细请查看权威指南211页  
			btStateUp.setActualSize(upAsset.width+margin,upAsset.height+margin);  
			btStateDown.setActualSize(downAsset.width+margin,downAsset.height+margin);  
			var pixelsFromTop:int = -23;  
			var pixelsFromRight:int = 10;  
			var buttonWidth:int = btStateUp.width;  
			var x:Number = unscaledWidth - buttonWidth - pixelsFromRight;  
			var y:Number = pixelsFromTop;  
			btStateDown.move(x,y);  
			btStateUp.move(x,y);  
		}  
		public function maximize():void{  
			
			tHeight = this.height;
			this.height = 28;
			var len:int=this.numElements;
			for(var i:int=0; i<len-1;i++){
				this.getElementAt(i).visible = false;
			}
			btStateUp.visible = false;  
			btStateDown.visible = true; 
		}
		private function doMaximize(event:Event):void{  
			//setState(1);
			maximize();
			
		}  
		
		private function doRestore(event:Event):void{  
			setState(0);  
			this.height = tHeight;
			btStateUp.visible = true;  
			btStateDown.visible = false; 
			
			var len:int=this.numElements;
			for(var i:int=0; i<len-1;i++){
				this.getElementAt(i).visible = true;
			}
		} 
		
	} 
		
	
}